package pl.edu.pw.polygen.modeler.client.graph;

import java.util.ArrayList;
import java.util.List;

/**
 * @author borysfan
 * @13-12-2011 @14:10:08
 */
public class GraphModel implements IGraphModel{
	private List<IVertexModel> vertexes;
	
	public GraphModel(){
		vertexes = new ArrayList<IVertexModel>();
	}

	@Override
	public void addVertex(IVertexModel vertexModel) {
		if(!vertexes.contains(vertexModel)){
			vertexes.add(vertexModel);
		}
	}

	@Override
	public void removeVertex(IVertexModel vertexModel) {
		for(IVertexModel v : vertexes){
			v.removeEdge(vertexModel);
		}
		if(vertexes.contains(vertexModel)){
			vertexes.remove(vertexModel);
		}
	}

	@Override
	public List<IVertexModel> getVertexes() {
		return vertexes;
	}

	@Override
	public IEdgeModel addEdge(IVertexModel begin, IVertexModel end, EdgeType type) {
		addVertex(begin);
		addVertex(end);
		IEdgeModel edgeModel = begin.addEdge(end, type);
//		end.addEdge(begin, type);
		return edgeModel;
	}

	@Override
	public void removeEdge(IVertexModel begin, IVertexModel end, EdgeType type) {
		begin.removeEdge(end, type);
	}

	@Override
	public void removeEdge(IEdgeModel edgeModel) {
		IVertexModel begin = edgeModel.getBegin();
		IVertexModel end = edgeModel.getEnd();
		EdgeType type = edgeModel.getType();
		begin.removeEdge(end,type);
		end.removeEdge(begin,type);
	}

	public void clear() {
		vertexes.clear();
	}
	
}
